<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Session;
class CartController extends Controller
{
    public function index(Request $request){
        $uid = isset(session('home')->id) ? session('home')->id : 0;
        if($uid>0){
            $data = DB::table('t_car')
                -> join('t_goods','t_goods.gid','=','t_car.gid')
                -> join('users','users.id','=','t_car.uid')
                -> select('t_car.count','t_goods.gname','t_goods.gprice','t_car.gid')
                -> where('t_car.uid',$uid)
                -> get();
            // 查询数据库中购物车的数据
            $goods_list = array();
            $total_Money = 0;//总价
            $total_Number = 0;//总数
            foreach($data as $v){
                // dd($v);
                foreach ($v as $k=>$u) {
                    $arr[$k] = $u;
                }
                $total_Money += $v->count *$v->gprice;
                $total_Number +=$v->count;
                $photo = DB::table('t_photo')
                    -> where('gid',$v->gid)
                    -> where('small','1')
                    -> first();
                    if($photo){
                        $arr['pic']=$photo->photo;
                    }
                $goods_list[$v->gid]=$arr;
            }
            // 将查询结果存到session
            
            session(['goods_list'=>$goods_list,'total_Money'=>$total_Money,'total_Number'=>$total_Number]);
            
        }

        // 将查询结果返回
        return view('home.cart.index'); 
    }
    public function ajax(Request $request){
        $uid = isset(session('home')->id) ? session('home')->id : 0;
        $data = DB::table('t_goods')
            -> where('gid',$request->gid)
            -> select('gname','gprice')
            -> first();
        $pic = DB::table('t_photo')
            -> where('gid',$request->gid)
            -> where('small','1')
            ->first();
            // ->photo;
        // dd($pic);
        if($pic){
            $pic =$pic->photo;
        }else{
            $pic='';
        }
        // 将session中的购物车数组拿出来
        $newList =  session('goods_list');
        // dd(!empty(session('goods_list')[$request->gid]));
        if(!empty(session('goods_list')[$request->gid])){
            // echo $newList[$request->gid]['count']."<br>";
            $newList[$request->gid]['count']+=$request -> count;

           
        }else{
            $newList[$request->gid] = ['gid'=>$request->gid,'gname'=>$data->gname,'count'=>$request -> count,'gprice'=> $data->gprice,'pic'=>$pic];
        }
        session(['goods_list'=>$newList]);
        if(session('home')->id>0){
            foreach(session('goods_list') as $k=>$v){
                $arr=['uid'=>session('home')->id,'count'=>$v['count'],'gid'=>$v['gid']];
                
            $r = DB::table('t_car')
                ->where('uid',$uid)
                ->whereIn('gid',[$v['gid']])
                ->get();
                // dd($v);
            if($r){
                $result = DB::table('t_car')
                    ->where('uid',$uid)
                    ->whereIn('gid',[$v['gid']])
                    ->update(['count'=>$v['count']]);
            }else{
                $result = DB::table('t_car')
                    ->insert($arr);

            }
            }
        }
        $total_Money = 0;//总价
        $total_Number = 0;//总数
        foreach(session('goods_list') as $v){
            $total_Number += $v['count'];
            $total_Money += ($v['count'] * $v['gprice']);
        }
        session(['total_Money'=>$total_Money,'total_Number'=>$total_Number]);
        if($request){
            return 1;
        }else{
            return 0;
        }
    }
}
